package jdbc;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import java.sql.ResultSet;
import java.util.Scanner;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: GodK
 * Date: 2024-06-01
 * Time: 12:41
 */
public class Testjdbc {
    public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        Scanner scanner = new Scanner(System.in);
        //String name = scanner.nextLine();
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/practice?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("123");
        Connection connection = dataSource.getConnection();
        //System.out.println(connection);
        String sqla = "select sum(salary) from depart,staff,salary where" +
                " depart.depart_id = staff.depart_id and staff.staff_id = salary.staff_id group by depart.depart_id and month = 6;";
        //String sql = "delete from staff where name = ?";
        String sqlb = "select depart.name,count(staff.name) from depart,staff where" +
                " depart.depart_id = staff.depart_id group by depart.name;";
        String sqlc = "select depart.name,salary.month,sum(salary.salary) from depart,staff,salary where" +
                " depart.depart_id = staff.depart_id and staff.staff_id = salary.staff_id group by month;";
        PreparedStatement statementa = connection.prepareStatement(sqla);
        PreparedStatement statementb = connection.prepareStatement(sqlb);
        PreparedStatement statementc = connection.prepareStatement(sqlc);
        ResultSet a = statementa.executeQuery();
        ResultSet b = statementa.executeQuery();
        ResultSet c = statementa.executeQuery();
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        statementa.close();
        statementb.close();
        statementc.close();
        connection.close();
    }
}
